﻿using Rabbit.Domain;
using Rabbit.DTO.Input;
using Rabbit.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rabbit.Service
{
    public class GoodsService
    {

        RabbitRepository<Goods> _gres;
        public GoodsService(RabbitRepository<Goods> gres)
        {
            _gres = gres;
        }
        /// <summary>
        /// 商品显示
        /// </summary>
        /// <returns></returns>
        public List<Goods> GetGoodsxlk()
        {
            return _gres.GetAll().Where(x=>x.IsDel!=1&&x.GState!=1).ToList();
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <returns></returns>
        public List<Goods> GetGoods()
        {
            return _gres.GetAll().Where(x => x.IsDel != 1).ToList();
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddGoods(GoodsInput input)
        {
            var list = _gres.GetAll().Where(x => x.GName == input.GName).ToList();
            if (list.Count() > 0)
            {
                return -1;
            }
            var lists = new Goods();
            lists.AddName = input.AddName;
            lists.GName= input.GName;
            lists.GState = input.GState;
            lists.AddDate=DateTime.Now;
            return _gres.Add(lists);
        }

        /// <summary>
        /// 逻辑删
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="names"></param>
        /// <returns></returns>
        public int DelGoods(string ids, string names)
        {
            var id = ids.Split(',');
            int[] ints = id.Select(x => int.Parse(x.Trim())).ToArray();
            var list = _gres.GetAll().Where(x => ints.Contains(x.Gid)).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelName = names;
                item.DelDate = DateTime.Now;
            }
            return _gres.Update(list);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdGoods(Goods entity)
        {
            var list = _gres.GetAll().Where(x => x.Gid == entity.Gid).ToList();
            foreach (var item in list)
            {
                item.AddName = entity.AddName;
                item.AddDate = entity.AddDate;
                item.UpdDate = DateTime.Now;
                item.UpdName = entity.UpdName;
                item.DelDate = null;
                item.DelName = null;
                item.IsDel = entity.IsDel;
                item.GName = entity.GName;
                item.GState= entity.GState;
            }
            return _gres.Update(list);
        }




    }
}
